package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sc.nft.entity.UserWallet;
import com.sc.nft.entity.minimall.MallUserWallet;
import com.sc.nft.mapper.MallUserWalletMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

/**
 * <p>
 * 商城用户钱包 DAO
 * </p>
 *
 * @author author
 * @since 2024-05-20
 */
@Slf4j
@Repository
public class MallUserWalletDao extends ServiceImpl<MallUserWalletMapper, MallUserWallet> {

    /**
     * 获取用户钱包
     *
     * @param miniUserId
     * @return
     */
    public MallUserWallet getByMiniUserId(Long miniUserId) {
        LambdaQueryWrapper<MallUserWallet> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallUserWallet::getMiniUserId, miniUserId);
        wrapper.orderByDesc(MallUserWallet::getId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    /**
     * 添加收入
     *
     * @param id
     * @param amount
     * @return
     */
    public Boolean addRevenue(Long id, BigDecimal amount) {
        LambdaUpdateWrapper<MallUserWallet> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("balance=balance+" + amount + ",sum_income=sum_income+" + amount);
        wrapper.eq(MallUserWallet::getId, id);
        return update(new MallUserWallet(), wrapper);
    }

    /**
     * 添加提现金额(用户申请提现)
     *
     * @param id
     * @param amount
     * @return
     */
    public Boolean addWithdrawingAmount(Long id, BigDecimal amount) {
        LambdaUpdateWrapper<MallUserWallet> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("withdrawing_amount=withdrawing_amount+" + amount + ",balance=balance-" + amount);
        wrapper.eq(MallUserWallet::getId, id);
        return update(new MallUserWallet(), wrapper);
    }

    /**
     * 提现成功
     *
     * @param id
     * @param amount
     * @return
     */
    public Boolean addSumWithdraw(Long id, BigDecimal amount) {
        LambdaUpdateWrapper<MallUserWallet> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("sum_withdraw=sum_withdraw+" + amount + ",withdrawing_amount=withdrawing_amount-" + amount);
        wrapper.eq(MallUserWallet::getId, id);
        return update(new MallUserWallet(), wrapper);
    }

    /**
     * 提现失败
     *
     * @param id
     * @param amount
     * @return
     */
    public Boolean subWithdrawingAmount(Long id, BigDecimal amount) {
        LambdaUpdateWrapper<MallUserWallet> wrapper = new LambdaUpdateWrapper<>();
        wrapper.setSql("withdrawing_amount=withdrawing_amount-" + amount + ",balance=balance+" + amount);
        wrapper.eq(MallUserWallet::getId, id);
        return update(new MallUserWallet(), wrapper);
    }
}
